package com.example.event_resource_java.trainapi.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.event_resource_java.commons.base.PageVo;
import com.example.event_resource_java.commons.util.ApiResult;
import com.example.event_resource_java.trainapi.dao.DictDO;
import com.example.event_resource_java.trainapi.dao.vo.CoursesVo;
import com.example.event_resource_java.trainapi.dao.vo.TrainingResourcesVo;
import com.example.event_resource_java.trainapi.service.CoursesService;
import com.example.event_resource_java.trainapi.service.DictService;
import com.example.event_resource_java.trainapi.service.TrainingResourcesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/course")
@CrossOrigin
public class CoursesController {

    @Autowired
    private TrainingResourcesService trainingResourcesService;
    @Autowired
    private DictService dictService;
    @Autowired
    private CoursesService coursesService;

//    private static final String VALID_API_KEY = "02817565662714ED";
    /**
     * @auther:
     * @Description 查询课程接口
     * @param: jz_courses
     */
    @GetMapping("/getCourses")
    public ApiResult getCourses(@RequestParam(value = "apiKey") String apiKey,
                                @RequestParam(value = "name",required = false) String name,
                                @RequestParam(value = "level",required = false) String level){
            //校验md5密钥 去查询数据字典有没有值，有值就返回
            DictDO type = dictService.getOne(new QueryWrapper<DictDO>().eq("type", apiKey));
            if (type != null) {
                List<CoursesVo> trains = coursesService.getCourses(name, level);
                return ApiResult.success(trains);
            } else {
                // 密钥无效，返回未授权响应
                return ApiResult.fail("密钥不正确");
            }
    }


    /**
     * @auther:
     * @Description 获取课程详情
     * @param:
     */
    @GetMapping("/getCoursesById")
    public ApiResult getCoursesById(@RequestParam(value = "id") Long id){
        CoursesVo vo= coursesService.getCoursesById(id);
        return ApiResult.success(vo);

    }
}
